<?php
	require_once(realpath(dirname(__FILE__)) . "/Baseobj.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Caracteristiquedesc.class.php");

	class Caracteristique extends Baseobj{

		var $id;
		var $affiche;
		var $classement;

		const TABLE="caracteristique";
		var $table=self::TABLE;

		var $bddvars = array("id", "affiche", "classement");

		function __construct($id = 0){
			parent::__construct();

			if($id > 0)
 			  $this->charger($id);
		}

		function charger($id){
			return $this->getVars("select * from $this->table where id=\"$id\"");
		}

		function changer_classement($id, $type){

			$this->charger($id);
			$remplace = new Caracteristique();

			if($type == "M")
				$res = $remplace->getVars("select * from $this->table where classement<" . $this->classement . " order by classement desc limit 0,1");

			else if($type == "D")
				$res  = $remplace->getVars("select * from $this->table where classement>" . $this->classement . " order by classement limit 0,1");

			if(! $res)
				return "";

			$sauv = $remplace->classement;

			$remplace->classement = $this->classement;
			$this->classement = $sauv;

            $remplace->maj();
            $this->maj();

		}

		function delete($requete){
				$resul = mysql_query($requete);
				CacheBase::getCache()->reset_cache();				
		}

		function supprimer(){

            if($this->id == "")
                    return 0;

			$caracteristiquedesc =  new Caracteristiquedesc();


			$this->delete("delete from $this->table where id=\"$this->id\"");
			$this->delete("delete from $caracteristiquedesc->table where caracteristique=\"$this->id\"");

			$queryclass = "select * from $this->table order by classement";
			$resclass = mysql_query($queryclass);

			if(mysql_num_rows($resclass) > 0){
				$i=1;
				while($rowclass = mysql_fetch_object($resclass)){
					$carac = new Caracteristique();
					$carac->charger($rowclass->id);
					$carac->classement = $i;
					$carac->maj();
					$i++;
				}
			}


			return 1;

		}

	}
?>